<template>
  <transition name="slid-left">
    <music-list :rank="rank" :title="toplist.topTitle" :avatar="imgUrl" :songs="songs"></music-list>
  </transition>
</template>

<script type="text/ecmascript-6">
import { mapGetters } from 'vuex'
import MusicList from 'components/music-list/music-list'
import { getTopMusicList } from 'api/rank'
import { ERR_OK } from 'api/config'
import { createSong } from 'common/js/song'

export default {
  name: 'top-list',
  components: {
    MusicList
  },
  data() {
    return {
      songs: [],
      rank: true
    }
  },
  computed: {
    imgUrl() {
      if (this.songs.length > 0) {
        return this.songs[0].image
      } else {
        return ''
      }
    },
    ...mapGetters({
      toplist: 'toplist'
    })
  },
  methods: {
    _normalizeSongs(list) {
      let ret = []
      list.forEach((item) => {
        let { data } = item
        if (data.songid && data.albummid) {
          ret.push(createSong(data))
        }
      })
      return ret
    }
  },
  created() {
    if (!this.toplist) this.$router.push('/rank')
    getTopMusicList(this.toplist.id).then((resp) => {
      if (resp.code === ERR_OK) {
        this.songs = this._normalizeSongs(resp.songlist)
      }
    })
  }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.slid-left-enter-active, .slid-left-leave-active
  transition: all .6s
.slid-left-enter, .slid-left-leave-to
  transform: translate3d(100%, 0, 0)

</style>
